package com.zl.learning.db.redis;

import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

public class TestRedis2 {

    private JedisPool jedisPool = init();

    private JedisPool init() {
        JedisPoolConfig config = new JedisPoolConfig();
//        return new JedisPool(config, "localhost", 6379);
        return new JedisPool(config, "172.31.4.247", 6379);
    }

    private void execute(BiConsumer<Jedis, Pipeline> consumer) {
        try {
            Jedis jedis = jedisPool.getResource();
            Pipeline pipeline = jedis.pipelined();
            consumer.accept(jedis, pipeline);
            pipeline.close();
            jedis.close();
        } catch (Exception e) {
            jedisPool = init();
        }
    }

    @Test
    public void test1() {
        Jedis jedis = jedisPool.getResource();
        jedis.set("aa", "aa");
        String val = jedis.get("aa");
        Assert.assertEquals("aa", val);
    }

    @Test
    public void test2() {
        String a = "a";
        String b = "a";
        String c = new String("a");
        System.out.println(a == c);
        System.out.println(a == "a");
    }

    @Test
    public void test3() {
        int COUNT_BITS = Integer.SIZE - 3;
        int CAPACITY = (1 << COUNT_BITS) - 1;
        int a = 100 & CAPACITY;
        System.out.println(a);
    }

    @Test
    public void test4() {
        for (int i = 0; i < 1000; i++) {
            execute((jedis, pipeline) -> {
                try {
                    System.out.println("enter...");
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println(jedis.isConnected());
                    String val = jedis.get("a");
                    System.out.println(val);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

    @Test
    public void test5() {
        execute(((jedis, pipeline) -> {
            try {
                boolean isConnnect = jedis.isConnected();
                System.out.println("Enter ... ");
                System.out.println(isConnnect);

                TimeUnit.MINUTES.sleep(1);

                jedis.set("a", "a");

                System.out.println("Leave ... ");
                isConnnect = jedis.isConnected();
                System.out.println(isConnnect);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
    }

    @Test
    public void test6() {
        Jedis jedis = jedisPool.getResource();
//        jedis.sadd("aa".getBytes(), JSON.toJSONBytes(new User(1, "zhangsan")));
        jedis.select(1);
        Set<String> set = new HashSet<>();
        String[] _res = set.toArray(new String[]{});
        Set<String> res = jedis.sunion(_res);
        System.out.println(res);
        jedis.close();
    }

    @Test
    public void test7() throws IOException {
        Jedis jedis = jedisPool.getResource();
        User user = new User(11, "zhangsan");

        final ByteArrayOutputStream out1 = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(out1);
        out.writeObject(user);
        final byte[] bytes = out1.toByteArray();
        jedis.set("u1".getBytes(), bytes);

    }

    @Test
    public void test8() throws IOException, ClassNotFoundException {
        Jedis jedis = jedisPool.getResource();
        final byte[] bytes1 = jedis.get("u1".getBytes());
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes1));
        User user = (User) in.readObject();
        System.out.println(user);
    }
}
